﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml;
using RN.ERP.Web.BasicService;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            InitApplications();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer，则不会引发该事件。

        }

        /// <summary>
        /// 初始化系统所需数据。
        /// </summary>
        private void InitApplications()
        {
            ////加载系统编号
            Application[ApplicationConstant.APPLICATION_ID] = ConfigurationManager.AppSettings["AppId"];

            // 在应用程序启动时运行的代码。
            ////加载地区
            BasicServiceClient client = new BasicServiceClient();
            ///获取系统配置
            SysApplication appconfig = JsonConvert.DeserializeObject<SysApplication>(client.GetApplicationConfig(ConfigurationManager.AppSettings["AppId"].ToString(), string.Empty));
            Application[ApplicationConstant.APPLICATION_SETTING] = appconfig;
            //////获取地区
            //Application[ApplicationConstant.APPLICATION_AREA] = client.GetCity();
            //////获取专家分库
            //Application[ApplicationConstant.APPLICATION_EXPERTDBCODE] = client.GetExpertDBList();
            //////获取企业所有权性质CorpType字典
            //var CorpType = client.GetDictionary("CorpType", "", "");
            //Application[ApplicationConstant.APPLICATION_CORPTYPE] = CorpType;
            //////获取企业类型
            ////var CorpUserType = client.GetDictionary("CorpUserType", "", "");
            //var CorpUserType = appconfig == null ? client.GetDictionary("CorpUserType", "", "") : appconfig.SiteConfig.RegisterConfig.CorpUserType;
            //Application[ApplicationConstant.APPLICATION_CORPUSERTYPE] = CorpUserType == null ? string.Empty : CorpUserType;
            //////证件类型
            //var CertificateType = client.GetDictionary("CertificateType", "", "");
            //Application[ApplicationConstant.APPLICATION_CERTIFICATETYPE] = CertificateType;
            ////功能页面映射关系。
            var funcPageMapping = JsonConvert.DeserializeObject<List<ModelService.T_Sys_FuncPageMapping>>(client.GetFuncPageMapping(Application[ApplicationConstant.APPLICATION_ID].ToString()));
            Application[ApplicationConstant.APPLICATION_FUNCPAGEMAPPING] = funcPageMapping;
            ////初始化错误配置信息。
            InitErrorAppliication();
            /////初始化c++跳转配置
            //InitAreaUrlSet();
        }

        /// <summary>
        /// 初始化错误配置信息。
        /// </summary>
        private void InitErrorAppliication()
        {
            List<ApplicationErrorEntity> errors = new List<ApplicationErrorEntity>();
            XmlDocument errorDocument = new XmlDocument();
            errorDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "LoadFiles\\ErrorInfo.xml");
            XmlElement root = errorDocument.DocumentElement;
            XmlNodeList errorList = errorDocument.SelectNodes("/Errors/Error");
            foreach (XmlNode errorNode in errorList)
            {
                ApplicationErrorEntity error = new ApplicationErrorEntity();
                error.Title = errorNode.Attributes["Title"].Value;
                error.Code = errorNode.Attributes["Code"].Value;
                error.Text = errorNode.Attributes["Text"].Value;
                error.IsReturnToIndex = errorNode.Attributes["Text"].Value.ToLower() == "true";
                errors.Add(error);
            }
            Application[ApplicationConstant.APPLICATION_ERRORS] = errors;
        }

        /// <summary>
        /// 初始化c++调用页面配置信息
        /// </summary>
        public void InitAreaUrlSet()
        {
            List<AreaSet> list = new List<AreaSet>();
            XmlDocument setDoc = new XmlDocument();
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "EnterpriseForCpp\\AreaUrlSettings.xml";
            if (File.Exists(filePath))
            {
                setDoc.Load(filePath);
                XmlNodeList areaNodes = setDoc.SelectNodes("/Set/Area");
                foreach (XmlNode areaNode in areaNodes)
                {
                    AreaSet areaSet = new AreaSet();
                    areaSet.AreaCode = areaNode.Attributes["Code"] == null ? string.Empty : areaNode.Attributes["Code"].Value;
                    areaSet.Title = areaNode.Attributes["Title"] == null ? string.Empty : areaNode.Attributes["Title"].Value;
                    areaSet.Remark = areaNode.Attributes["Remark"] == null ? string.Empty : areaNode.Attributes["Remark"].Value;
                    XmlNodeList kindNodes = areaNode.SelectNodes("Kind");
                    List<KindSet> kindList = new List<KindSet>();
                    foreach (XmlNode kindNode in kindNodes)
                    {
                        KindSet kindSet = new KindSet();
                        kindSet.KindId = kindNode.Attributes["KindId"] == null ? string.Empty : kindNode.Attributes["KindId"].Value;
                        kindSet.Title = kindNode.Attributes["Title"] == null ? string.Empty : kindNode.Attributes["Title"].Value;
                        kindSet.Url = kindNode.Attributes["Url"] == null ? string.Empty : kindNode.Attributes["Url"].Value;
                        kindList.Add(kindSet);
                    }
                    areaSet.KindSets = kindList;
                    list.Add(areaSet);
                }
                Application[ApplicationConstant.APPLICATION_AREAURLSET] = list;
            }
        }
    }
}
